// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { colliderType } from "../../../script/game/gameType";
import { ccs } from "../../../script/util/services";

const {ccclass, property} = cc._decorator;

@ccclass
export default class money extends cc.Component {
   
    @property(cc.Label) 
    money: cc.Label = null;

    @property(cc.Integer)
    isSelf: number = 1;

    count: number = 0;
    start () {
        let collider = this.node.addComponent(cc.BoxCollider);
        collider.size.width = 138;
        collider.size.height = 50;
        this.node.getComponent(cc.BoxCollider).tag = this.isSelf? colliderType.myMoney: colliderType.otherMoney;
    }

    setMoney(money: number) {
        this.money.string = ccs.gf.getOmitNum(money);
        this.count = money;
    }
    onCollisionEnter(other, self) {
        let collider = other.getComponent(cc.BoxCollider);
        if(collider.tag == colliderType.myHome && !this.isSelf) { 
            // 对面的子弹
            ccs.mainEmitter.emit("hurtSelf");
            ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
            ccs.mainEmitter.emit("robotAttack");
        } else if(collider.tag === colliderType.otherHome && this.isSelf) {
            // 自己的子弹
            ccs.mainEmitter.emit("hurtOther");
            ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
        } else if(collider.tag === colliderType.otherMoney) {
            let count = other.node.getComponent(money).count;
            // if(this.count > count) {
            //     //抵消敌人的
            //     if(!this.isSelf) {
            //         // ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
            //         // ccs.mainEmitter.emit("hurtOther");
            //     }
            // } else 
            if(this.count < count) {
                //被抵消
                    // ccs.mainEmitter.emit("hurtSelf");
                ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
            } else if(this.count === count) {
                ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
            }
            // 自己的攻击和敌人的抵消
        } else if(collider.tag === colliderType.myMoney) {
            //对面的碰撞检测
            let count = other.node.getComponent(money).count;
            // if(this.count > count) {
            //     ccs.mainEmitter.emit("hurtSelf");
            // } else 
            if(this.count < count) {
                ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
                // ccs.mainEmitter.emit("hurtSelf");
            } else if(this.count === count) {
                ccs.mainEmitter.emit("dismiss", this.node, this.isSelf);
            }
        }
        // 抵消了
    }
    // update (dt) {}
}
